
package com.jf.cloud.multishop.controller.admin;

import com.jf.cloud.api.multishop.vo.ShopRefundAddrVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.dto.ShopRefundAddrDTO;
import com.jf.cloud.multishop.model.ShopRefundAddr;
import com.jf.cloud.multishop.service.ShopRefundAddrService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 退货地址
 *
 * @author zz
 * @date 2021-01-27 09:10:00
 */
@RestController("adminShopRefundAddrController")
@RequestMapping("/mp/shop_refund_addr")
@Tag(name = "admin-退货地址")
public class ShopRefundAddrController {

    @Autowired
    private ShopRefundAddrService shopRefundAddrService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "分页获取退货地址列表" , description = "分页获取退货地址列表")
    public ServerResponseEntity<PageVO<ShopRefundAddrVO>> page(@Valid PageDTO pageDTO, @ParameterObject ShopRefundAddrDTO shopRefundAddrDTO) {
        shopRefundAddrDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<ShopRefundAddrVO> refundAddrPage = shopRefundAddrService.page(pageDTO, shopRefundAddrDTO);
        return ServerResponseEntity.success(refundAddrPage);
    }

    @GetMapping
    @Operation(summary = "获取退货地址" , description = "根据shopRefundAddrId获取退货地址")
    public ServerResponseEntity<ShopRefundAddrVO> getByShopRefundAddrId(@ParameterObject @RequestParam Long shopRefundAddrId) {
        Long shopId = AuthUserContext.get().getTenantId();
        ShopRefundAddrVO byShopRefund = shopRefundAddrService.getByShopRefundAddrId(shopRefundAddrId, shopId);
        return ServerResponseEntity.success(byShopRefund);
    }

    @PostMapping
    @Operation(summary = "保存退货地址" , description = "保存退货地址")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ShopRefundAddrDTO shopRefundAddrDTO) {
        ShopRefundAddr shopRefundAddr = mapperFacade.map(shopRefundAddrDTO, ShopRefundAddr.class);
        Long shopId = AuthUserContext.get().getTenantId();
        shopRefundAddr.setShopId(shopId);
        shopRefundAddrService.save(shopRefundAddr);
        shopRefundAddrService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新退货地址" , description = "更新退货地址")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopRefundAddrDTO shopRefundAddrDTO) {
        ShopRefundAddr shopRefundAddr = mapperFacade.map(shopRefundAddrDTO, ShopRefundAddr.class);
        Long shopId = AuthUserContext.get().getTenantId();
        shopRefundAddr.setShopId(AuthUserContext.get().getTenantId());
        shopRefundAddrService.update(shopRefundAddr);
        shopRefundAddrService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除退货地址" , description = "根据退货地址id删除退货地址")
    public ServerResponseEntity<Void> delete(@RequestParam Long shopRefundAddrId) {
        Long shopId = AuthUserContext.get().getTenantId();
        shopRefundAddrService.deleteById(shopRefundAddrId, shopId);
        shopRefundAddrService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }
    @GetMapping("/list")
    @Operation(summary = "获取退货地址列表" , description = "获取退货地址列表")
    public ServerResponseEntity<List<ShopRefundAddrVO>> list() {
        return ServerResponseEntity.success(shopRefundAddrService.listByShopId(AuthUserContext.get().getTenantId()));
    }
}
